package com.xuetang9.t9.hotel.controller;

import com.xuetang9.t9.hotel.beans.EFood;
import com.xuetang9.t9.hotel.mapper.condition.impl.FoodCondition;
import com.xuetang9.t9.hotel.services.IFoodService;
import com.xuetang9.t9.hotel.services.IGoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName FoodController
 * @Description TODO
 * @Author qin
 * @Date 2020/2/24 15:23
 * @Version 1.0
 **/
@Controller
@RequestMapping("food")
public class FoodController {
    @Autowired
    private IFoodService foodService;
    @Autowired
    private IGoryService goryService;

    private int nowPage=1;
    private int size=5;
    private int allPage;

    public List<EFood> list(int id){
        allPage = foodService.countFood()%size==0?(foodService.countFood()/size):(foodService.countFood()/size)+1;
        if (id==1){
            nowPage=1;
        }else if (id==2){
            nowPage = nowPage-1<=0?1:nowPage-1;
        }else if (id==3){
            nowPage = nowPage+1>=allPage?allPage:nowPage+1;
        }else if (id==4){
            nowPage = allPage;
        }
        return foodService.foodList(nowPage,size);
    }

    @GetMapping("list")
    @ResponseBody
    public List<EFood> allFood(){
        return foodService.queryAll();
    }

    @PostMapping("gory")
    @ResponseBody
    public List<EFood> goryFood(@RequestParam("categoryId") Long categoryId) {
        System.out.println(categoryId);
        return foodService.queryByCategoryId(categoryId);
    }

    @PostMapping("first")
    @ResponseBody
    public List<EFood> first(){
        return list(1);
    }

    @PostMapping("up")
    @ResponseBody
    public List<EFood> up(){
        return list(2);
    }
    @PostMapping("down")
    @ResponseBody
    public List<EFood> down(){
        return list(3);
    }
    @PostMapping("last")
    public List<EFood> last(){
        return list(4);
    }



    @PostMapping("insert")
    @ResponseBody
    public List<EFood> insertFood(@RequestParam("name")String name,@RequestParam("desc")String desc,@RequestParam("price")String price,@RequestParam("cost")String cost,@RequestParam("gory")String gory,@RequestParam("inventoryCount")String inventoryCount,@RequestParam("pic")String picPath){
        EFood food = new EFood(name,desc,Double.valueOf(price),Double.valueOf(cost),Long.valueOf(gory),Long.valueOf(inventoryCount),picPath);
        foodService.insertFood(food);
        return first();
    }

    @PostMapping("update")
    @ResponseBody
    public List<EFood> insertFood(@RequestParam("name")String name,@RequestParam("desc")String desc,@RequestParam("price")String price,@RequestParam("cost")String cost,@RequestParam("gory")String gory,@RequestParam("inventoryCount")String inventoryCount,@RequestParam("id")String id,@RequestParam("pic")String picPath){
        EFood food = new EFood(Long.valueOf(id),name,desc,Double.valueOf(price),Double.valueOf(cost),Long.valueOf(gory),Long.valueOf(inventoryCount),picPath);
        foodService.updateFood(food);
        return foodService.foodList(nowPage,size);
    }

    @PostMapping("delete")
    @ResponseBody
    public List<EFood> deleteFood(@RequestParam("id") String id){
        System.out.println(id);
        foodService.deleteFood(Integer.valueOf(id));
        return foodService.foodList(nowPage,size);
    }

    @PostMapping(value = "picLoad",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String picUpload(@RequestParam("file") MultipartFile upload, HttpServletRequest request) throws IOException {
        return saveFile(upload, request);
    }
    @RequestMapping(value = "/show",method = RequestMethod.POST)
    @ResponseBody
    private Map show(@RequestBody FoodCondition condition){
        Map map = getMap(condition);
        return map;
    }

    private String saveFile(MultipartFile upload,HttpServletRequest request) throws IOException {
        if (!upload.isEmpty()){
            String  filePath = request.getSession().getServletContext().getRealPath("/upload/");
            File file = new File(filePath);
            if (!file.exists()){
                file.mkdirs();
            }
            String filename = upload.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replace("-","");
            filename = uuid+"-"+filename;
            File file1 = new File(filePath,filename);
            upload.transferTo(file1);
            String path = file1.getAbsolutePath().substring(file1.getAbsolutePath().lastIndexOf("\\upload"));
            System.out.println(path.replaceAll(  "\\\\",   "/"));
            return path.replaceAll(  "\\\\",   "/");
        }else {
            return "";
        }
    }
    @PostMapping("analysis")
    @ResponseBody
    private List<EFood> analysis(){
        return foodService.analysisFood();
    }
    @PostMapping("one")
    @ResponseBody
    private EFood one(@RequestParam("id") Integer id){
        System.out.println(id);
        return foodService.one(id);
    }


    @RequestMapping(value = "/updateclean",method = RequestMethod.POST)
    @ResponseBody
    private Map updateByClean(@RequestBody FoodCondition condition){
        foodService.updateByClean(condition);
        condition.setId(null);
        Map map = getMap(condition);
        return map;
    }


    private Map getMap(@RequestBody FoodCondition condition) {
        Map map = new HashMap();
        map.put("datas", foodService.queryByAll(condition));
        condition.setClean(0);
        map.put("onCount", foodService.selectByCount(condition));
        condition.setClean(1);
        map.put("yesCount", foodService.selectByCount(condition));
        return map;
    }

    @GetMapping("ECharts")
    @ResponseBody
    private Map EChartsData(){
        Map map = new HashMap();
        map.put("fname",foodService.queryFoodName());
        map.put("forder",foodService.queryFoodOrder());
        map.put("gname",goryService.queryGoryName());
        map.put("gorder",goryService.queryGoryOrder());
        return map;
    }


}
